package com.example.a97369.startproject_306wkm.model;

import com.example.a97369.startproject_306wkm.bean.WeatherResult;
import com.example.a97369.startproject_306wkm.iface.WeatherListener;
import com.example.a97369.startproject_306wkm.iface.Weatheriface;
import com.example.a97369.startproject_306wkm.service.WeatherService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 97369 on 2017/3/14.
 */

public class WeatherModel implements Weatheriface {
    //retrofit实例化
    private Retrofit retrofit;
    private String BASEURL="http://www.weather.com.cn/";
    public WeatherModel() {
        retrofit=new Retrofit.Builder().baseUrl(BASEURL)//主机地址
                                       .addConverterFactory(GsonConverterFactory.create())//解析方式
                                       .build();

    }

    @Override
    public void getWeather(String cityno, final WeatherListener listener) {
      //Retrofit接口使用3-2
        WeatherService weatherService = retrofit.create(WeatherService.class);
        Call<WeatherResult> call=
                weatherService.getResult(cityno);
        //Retrofit接口使用3-3
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
               if(response.isSuccessful()&&response.body()!=null)
                listener.onResponse(response.body().getWeatherinfo());
               else
                   listener.omFail("OnResponse error");
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {

            }
        });
    }
}
